<template>
  <div>
    <el-card class="search-card">
      <basic-form
        ref="formData"
        defaultExpandStatus
        :hasQueryPermission="false"
        :formColumns="formItems"
        :dataSource="formData"
        :handleSearchForm="handleQueryForm"
        :handleClearForm="clearSupplierInfo"
        @getFormData="getFormData"
      >
        <RemoteSelect
          ref="remoteSelect"
          slot="selectSupplier"
          placeholder="请选择供应商"
          :optionKeys="['supplierName', 'supplierNumber']"
          :echoValue="formData.supplyNo"
          :echoOptions="[formData.supplierName, formData.supplyNo]"
          :remoteSelectOptions="remoteOptionsBySupplier"
          :handleClickButton="handleChooseSupplier"
          @getSelect="updateSupplierInfo"
        />
      </basic-form>
    </el-card>
    <el-card class="table-card">
      <skus-table
        showPagination
        :tableHeight="480"
        :defaultMinWidth="100"
        :dataSource="TableInfo.list"
        :columns="TableInfo.columns"
        :loading="TableInfo.loading"
        :pageSizes="pageSizes"
        :pagination="pagination"
        :total="pagination.total"
        @pageChange="handlePageChange"
        @pageSizeSelect="handlePageSizeChange"
      >
        <BtnRender slot="content_context" :render="renderBtnsBar" />
      </skus-table>
    </el-card>
    <SelectSupplierDialog
      :visible.sync="dialogVisible.supplier"
      @getSupplierInfo="updateSupplierInfo"
    />
  </div>
</template>
<script>
import BasicForm from '@/components/BasicForm';
import SkusTable from '@/components/skusTable';
import BtnRender from '@/components/JSXRender/BtnRender';
import RemoteSelect from '@/pages/commodity/components/RemoteSelect';
import SelectSupplierDialog from './components/SelectSupplierDialog';
import skusPage from '@/mixins/commodity/skusPage';
import { FORM_ITEMS, TABLE_COLUMNS } from './constants';
import { getInitFormData, TableInfo } from '@/utils/getUtils';
import { getListPageBtnsBar } from '@/utils/getBtnsRender';
import { setAuthBtnId, createExcelByExport, generateUuid } from '@/utils/bizUtils';
import {
  getCollectPotentialProductPriceInfoService,
  exportCollectPotentialProductPriceInfoService,
  getAllSupplierMainInfoService
} from '@/api/commodity/temporaryPurchasingGoods/recordClueLatentSalePriceInfo';
import { queryAreaInfoService } from '@/api/public';
export default {
  mixins: [skusPage],
  components: {
    BasicForm,
    SkusTable,
    BtnRender,
    RemoteSelect,
    SelectSupplierDialog,
  },
  data () {
    return {
      formItems: FORM_ITEMS,
      formData: getInitFormData(FORM_ITEMS),
      TableInfo: new TableInfo({ columns: TABLE_COLUMNS }),
      pageSizes: [10, 100, 1000],
      dialogVisible: {
        supplier: false
      },
      btnLoading: {
        export: false
      }
    }
  },
  created () {
    this.setProvinceOptions()
    this.getTableData()
  },
  methods: {
    // 渲染按钮操作栏
    renderBtnsBar (h) {
      const finalBtns = [
        {
          label: '导出',
          method: this.handleExport,
          loading: this.btnLoading.export,
          hasAuth: true
        }
      ]
      return getListPageBtnsBar(h, finalBtns, this.showBtn, 'start')
    },
    // 获取表格数据
    async getTableData () {
      setAuthBtnId('查询')
      const data = { ...this.formData, ...this.paginationParams }
      const res = await getCollectPotentialProductPriceInfoService(data, this.TableInfo.setLoading)
      this.TableInfo.setList(res.retData?.results)
      this.setTotalNum(res.retData?.totalNum)
    },
    // 获取省份下拉集合
    async setProvinceOptions () {
      const res = await queryAreaInfoService()
      const areaInfos = res?.retData ?? []
      const provinceOptions = areaInfos.map(item => ({
        value: item.provinceCode,
        label: item.provinceName
      }))
      const index = this.formItems.findIndex(item => item.prop == 'provinceCode')
      if (index > -1) this.formItems[index].options = provinceOptions
    },
    // 导出
    async handleExport () {
      setAuthBtnId('导出')
      const data = { ...this.formData }
      const loadingFn = state => { this.btnLoading.export = state }
      const res = await exportCollectPotentialProductPriceInfoService(data, loadingFn)
      createExcelByExport({ res, excelTitle: `记录线索潜在商品售价信息_${generateUuid()}` })
    },
    // 选择供应商
    handleChooseSupplier () {
      this.dialogVisible.supplier = true;
    },
    // 远程搜索供应商
    async remoteOptionsBySupplier (queryString, loadingFn) {
      const data = {
        nowPage: 1,
        pageShow: 20,
        supplierName: queryString
      }
      const res = await getAllSupplierMainInfoService(data, loadingFn)
      return res?.retData?.results ?? []
    },
    // 更新弹框选择的供应商信息
    updateSupplierInfo (data) {
      this.formData.supplierName = data.supplierName
      this.formData.supplyNo = data.supplierNumber
    },
    // 清空供应商信息
    clearSupplierInfo () {
      this.formData.supplierName = ''
      this.$refs.remoteSelect.clearValue()
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .table-action {
  margin-right: 0;
}
</style>
